package com.hulk.dryad.activiti7.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hulk.dryad.activiti7.api.vo.WorkflowLeaveVO;
import com.hulk.dryad.activiti7.mapstruct.WorkflowLeaveStruct;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.annotation.RespExcel;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.util.SecurityUtils;
import com.hulk.dryad.persistence.entity.WorkflowLeave;
import com.hulk.dryad.persistence.service.IWorkflowLeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *
 *
 * @author hulk
 * @date 2020-10-28
 */
@Api(value = "task", tags = "请假工作流")
@AllArgsConstructor
@RestController
@RequestMapping("/workflow/leave")
public class WorkflowLeaveController{

    private IWorkflowLeaveService workflowLeaveService;

    private WorkflowLeaveStruct workflowLeaveStruct;

    /**
     * 查询请假列表
     */
	@ApiOperation(value = "getPageLeave", notes = "查询请假列表")
	@PrintlnLog
    @GetMapping("/page")
    public R<IPage> getPageLeave( Page page ,WorkflowLeaveVO vo) {

		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
        workflowLeave.setCreatedBy(SecurityUtils.getUser().getUsername());
		List<WorkflowLeave> list = workflowLeaveService.selectWorkflowLeaveAndTaskNameList(page, workflowLeave);
		page.setRecords(list);
        return R.ok(page);
    }
    /**
     * 查询部门请假列表
     */
	@ApiOperation(value = "getPageLeaveDept", notes = "查询部门请假列表")
	@PrintlnLog
    @GetMapping("/page/dept")
    public R<IPage> getPageLeaveDept(Page page ,WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
		List<WorkflowLeave> list = workflowLeaveService.selectWorkflowLeaveList(page,workflowLeave);
		page.setRecords(list);
		return R.ok(page);
    }

    /**
     * 导出请假列表
     */
	@ApiOperation(value = "export", notes = "导出请假列表")
	@PreAuthorize("@pms.hasPermission('workflow:leave:export')")
	@RespExcel(name = "请假列表", sheet = "sheet1")
	@GetMapping("/export")
	public List<WorkflowLeaveVO> export(Page page, WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
		List<WorkflowLeave>  modelList = workflowLeaveService.selectWorkflowLeaveList(page,workflowLeave);
		List<WorkflowLeaveVO>  voList = workflowLeaveStruct.from(modelList);
		return voList;
	}
//	@ApiOperation(value = "export", notes = "导出请假列表")
//    @PreAuthorize("@pms.hasPermission('workflow:leave:export')")
//    @GetMapping("/export")
//    public void export(HttpServletResponse response,Page page, WorkflowLeaveVO vo) throws IOException{
//	      WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
//		List<WorkflowLeave>  modelList = workflowLeaveService.selectWorkflowLeaveList(page,workflowLeave);
//		List<WorkflowLeaveVO>  voList = workflowLeaveStruct.from(modelList);
//
//		response.setContentType("application/vnd.ms-excel");
//		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
//		String fileName = URLEncoder.encode("请假列表", StandardCharsets.UTF_8.name());
//		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//		EasyExcel.write(response.getOutputStream(), WorkflowLeaveVO.class).sheet("请假列表").doWrite(voList);
//    }

    /**
     * 获取请假详细信息
     */
	@ApiOperation(value = "getInfo", notes = "获取请假详细信息通过ID")
	@PrintlnLog
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
		WorkflowLeave  workflowLeave =workflowLeaveService.selectWorkflowLeaveById(id);
		return R.ok(workflowLeave);
    }

    /**
     * 获取请假详细信息
     */
	@ApiOperation(value = "getInfoByInstanceId", notes = "获取请假详细信息通过实例ID")
	@PrintlnLog
    @GetMapping(value = "/info/{instanceId}")
    public R getInfoByInstanceId(@PathVariable("instanceId") String instanceId) {
		WorkflowLeave  workflowLeave = workflowLeaveService.selectWorkflowLeaveByInstanceId(instanceId);
        return R.ok(workflowLeave);
    }

    /**
     * 新增请假
     */
    @PreAuthorize("@pms.hasPermission('workflow:leave:add')")
	@ApiOperation(value = "saveLeave", notes = "新增请假")
	@PrintlnLog
	@Idempotent
    @PostMapping
    public R save(@RequestBody WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
		int rows = (workflowLeaveService.insertWorkflowLeave(workflowLeave));
		return rows > 0 ? R.ok() : R.failed();
    }

    /**
     * 修改请假
     */
    @PreAuthorize("@pms.hasPermission('workflow:leave:edit')")
	@ApiOperation(value = "updateLeave", notes = "修改请假")
	@PrintlnLog
	@Idempotent
    @PutMapping
    public R update(@RequestBody WorkflowLeaveVO vo) {
		WorkflowLeave  workflowLeave =	workflowLeaveStruct.to(vo);
	    int rows =	workflowLeaveService.insertWorkflowLeave(workflowLeave);
        return rows > 0 ? R.ok() : R.failed();
    }

    /**
     * 删除请假
     */
    @PreAuthorize("@pms.hasPermission('workflow:leave:del')")
	@ApiOperation(value = "removeLeave", notes = "删除请假")
	@PrintlnLog
	@Idempotent
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
		int rows =	workflowLeaveService.deleteWorkflowLeaveByIds(ids);
        return  rows > 0 ? R.ok() : R.failed();
    }
}
